﻿using fuzhu;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using NETCore.MailKit.Core;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Org.BouncyCastle.Crypto;
using RestSharp;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using U8COWeb;
using XXX.api.Controllers;
using XXX.Bo;
using XXX.Common;
using XXX.Models;
using XXX.Models.AS;
using XXX.Models.GL;
using Yonyou.U8.BO;

namespace Yonyou.U8.YF.Api.WebApi.Controllers
{
    [Route("api/GLAccVouch")]
    [ApiController]
    [Authorize]
    public class ApiGLController : ControllerBase
    {
        public static readonly object _lockObj = new object();
        private readonly ILogger<ApiGLController> _logger;
        private readonly IEmailService _EmailService;

        public ApiGLController(ILogger<ApiGLController> logger, IEmailService emailService)
        {
            _logger = logger;
            _EmailService = emailService;
        }

        /// <summary>
        /// 凭证写入到U8,一张凭证调用一次
        /// </summary> 
        [HttpPost]
        public RetModel2 Post(JObject JsonObj)
        {
            //一次只执行一个线程
            lock (_lockObj)
            {
                try
                {
                    string requestBody = DbHelper.GetDbString(JsonObj);
                    //定义凭证实体，用于凭证操作
                    GLaccvouchVM glvm = new GLaccvouchVM();
                    #region 传入json验证
                    try
                    {
                        glvm = JsonConvert.DeserializeObject<GLaccvouchVM>(JsonObj.ToString());
                    }
                    catch (Exception e)
                    {
                        throw new Exception("json结构异常！" + e.Message);
                    }

                    if (glvm == null || glvm.body == null || glvm.para == null)
                    {
                        throw new Exception("json结构异常!数据不完整！");

                    }
                    #endregion

                    #region 接口调用间隔
                    string strKey = "GL_accvouch" + Md5Encrypt.MD5(requestBody);

                    if (CacheHelpCore.GetCacheValue(strKey) == null)
                    {
                        CacheHelpCore.SetChacheValue(strKey, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), CanShu.chkRepeat);
                    }
                    else
                    {
                        throw new Exception("接口调用间隔过短");
                    }
                    #endregion 接口调用间隔



                    #region 判断para参数的有效性，帐套号，登陆日期
                    paraEntity ls_pa = glvm.para;
                    if (glvm.para == null)
                    {
                        throw new Exception("para数据不可为空！");
                    }
                    else if (ls_pa.accid == null)
                    {
                        throw new Exception("账套号不可为空！");
                    }

                    //账套是001的，本币是港币HKD
                    if (ls_pa.accid=="001")
                    {
                        glvm.benbi= "HKD";
                    }
                    else
                    {
                        glvm.benbi = "人民币";
                    }
                    

                    if (glvm.body == null || glvm.body.Count == 0)
                    {
                        throw new Exception("body数据不可为空！");
                    }


                    RetModel2 retcon = DBService.GetConstrByaccount(ls_pa.accid);
                    if (retcon.code != 200)
                    {
                        throw new Exception(ls_pa.accid + retcon.message);
                    }
                    else
                    {
                        glvm.conStr = DbHelper.GetDbString(retcon.data);
                    }
                    #endregion



                    RetModel2 ret = glvm.pz_insert();

                    // 将 JSON 数据保存到数据库中
                    JsonSaveService.saveLog2("凭证导入", glvm.para.id, requestBody, ret.code, ret.message);
                    new SendEmailService(_EmailService, _logger).Send(CanShu.mail.SendCope, ret.code, CanShu.mail.ToAddress, glvm.para.accid + "凭证导入," + glvm.para.id, "报文:\r\n" + requestBody + "\r\n" + "结果,\r\n" + ret.message);

                    
                    return ret;



                }




                catch (Exception ex)
                {
                    //_EmailService.Send(CanShu.mail.ToAddress, CanShu.cZT + "部门创建错误,程序错误," + model.cDepCode, "报文:\r\n" + requestBody + "\r\n" + "结果,\r\n" + ex.Message);
                    return DBService.ReErrObject(ex.Message);
                }
            }


        }



       

        /// <summary>
        /// 凭证写入到U8
        /// </summary> 
        [HttpPost]
        [Route("insert")]
        public RetModel Insert(GLaccvouchVM JsonObj)
        {
            return DBService.ReOK(new List<object>());

        }

    }
}



